package starComponent.controls;

import java.awt.Component;
import java.util.ArrayList;

import starComponent.elements.A_StarBranch;
import starComponent.elements.FlyingNameLabelComparator;
import starComponent.elements.StarBranchBiSlider;
import starComponent.elements.StarBranchSlider;
import starComponent.elements.StarComponent;

/**
 * @author Charles Perin
 * 27 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class StarComparatorControler extends StarComponentControler implements ObserverStar{

	private StarComponentControler star1;


	private StarComponentControler star2;
	
	/**
	 * @param star l'etoile de comparaison
	 * @param star1 l'etoile modifiable dans la comparaison
	 * @param star2 l'etoile statique dans la comparaison
	 */
	public StarComparatorControler(StarComponent star, StarComponentControler star1, StarComponentControler star2) {
		super(star);
		this.star1 = star1;
		this.star2 = star2;	
		star1.addObserverStar(this);
		star2.addObserverStar(this);
		keepComparativeBranches();
	}

	public void keepComparativeBranches(){
		keepAllPossibleBranches();
		keepDisplayedBranches();
	}
	
	private void keepAllPossibleBranches(){
		for(A_StarBranch b1:star1.getAllPossibleBranchList()){
			for(A_StarBranch b2:star2.getAllPossibleBranchList()){
				if(b1.getName().equals(b2.getName())){
					allPossibleBranchList.add(b1);
				}
			}
		}
	}
	
	private void keepDisplayedBranches(){
		for(A_StarBranch b1:star1.getDisplayedBranchlist()){
			for(A_StarBranch b2:star2.getAllPossibleBranchList()){
				if(b1.getName().equals(b2.getName())){
					displayedBranchlist.add(b1);
				}
			}
		}
	}
	
	
	@Override
	public void createAllBranches() {
		ArrayList<A_StarBranch> safeDisplayedList = new ArrayList<A_StarBranch>(displayedBranchlist);
		for(A_StarBranch b:safeDisplayedList){
			b.updateBranch();
			FlyingNameLabelComparator fnl = new FlyingNameLabelComparator(b.getFlyingNameLabel(), star2);
			fnl.updateText();
			star.add(fnl);
			if(b instanceof StarBranchSlider)
				((StarBranchSlider)b).getThumb().addObserverThumb(fnl);
			else if(b instanceof StarBranchBiSlider){
				((StarBranchBiSlider)b).getThumbStart().addObserverThumb(fnl);
				((StarBranchBiSlider)b).getThumbEnd().addObserverThumb(fnl);
			}
		}
	}

	@Override
	public void update(StarComponentControler starComponentControler, int action) {
		if(starComponentControler == star1){
			switch(action){
			case ObservableStar.ADD_BRANCH:
				displayedBranchlist.clear();
				keepDisplayedBranches();
				createAllBranches();
				orderBranchesByAngle();
				break;
			case ObservableStar.REMOVE_BRANCH:
				for(Component comp : star.getComponents()){
					if(comp instanceof FlyingNameLabelComparator){
						System.out.println("labelcomparator spotted");
						star.remove(comp);
					}
				}
				displayedBranchlist.clear();
				keepDisplayedBranches();
				createAllBranches();
				orderBranchesByAngle();
				break;
			case ObservableStar.UPDATE_ALL:
				System.out.println("io");
				displayedBranchlist.clear();
				keepComparativeBranches();
				//createAllBranches();
				//orderBranchesByAngle();
				for(Component comp : star.getComponents()){
					if(comp instanceof FlyingNameLabelComparator){
						System.out.println("labelcomparator spotted");
						((FlyingNameLabelComparator)comp).getBranch().updateBranch();
						((FlyingNameLabelComparator)comp).updateText();
					}
				}
				break;
			}
		}
		else if(starComponentControler == star2){

		}
	}
	
	public StarComponentControler getStar1() {
		return star1;
	}

	public void setStar1(StarComponentControler star1) {
		this.star1 = star1;
	}

	public StarComponentControler getStar2() {
		return star2;
	}

	public void setStar2(StarComponentControler star2) {
		this.star2 = star2;
	}



}
